package core;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * TODO: write chat packet class, eg signing on with username, wisphering, get user list etc.
 * @author ciaran
 */
public class ProcessPacket extends Thread{
    DatagramPacket received = null;
    DatagramSocket serverSocket = null;
    byte[] buf = new byte[256];
    byte [] data = new byte[256];
    DatagramPacket reply = new DatagramPacket(buf, buf.length);
    
    public ProcessPacket(DatagramPacket received, DatagramSocket serverSocket){
        this.received = received;
        this.serverSocket = serverSocket;
    }
    
    @Override
    public void run(){
        data = received.getData();
        char mt = (char)data[0];    // MESSAGETYPE
        reply.setPort(3001);

        switch(mt){
            case 'c':   // Connect
                connect();
                break;
            case 'd':   // Disconnect
                disconnect();
                break;
            case 'b':   // BroadCast
                broadcast();
                break;
            case 'w':   // Whisper
                whisper();
                break;
        }
        
        try {
            serverSocket.send(reply);
        } catch (IOException ex) {
            System.err.println("Error IO Exception");
            System.exit(1);
        }
    }
    public void connect(){
        String username = readUserName();
        addString(username,1);
    /*    boolean connect = Server.clientConnect(username, received.getAddress());

        if(connect){
            buf[0] = 'c';
            String userlist = Server.getClientList();
            addString(userlist,1 + username.length() + 1);
            // TODO: Broadcast new connection
        }
        else{
           buf[0] = 'd';
           // TODO: send single message to user indicating connection failed
        }*/
    }

    public void disconnect(){
        String username = readUserName();
        addString(username,1);
       // boolean disconnect = Server.clientDisconnect(username, received.getAddress());
        /*if(disconnect){
            buf[0] = 'd';
            // TODO: Setup that packet is broadcasted
        }*/
    }

    public String readUserName(){
        StringBuilder username = new StringBuilder();
        int i = 1;
        char read = (char)data[i];
        while(read != '#'){
            username.append(read);
            i++;
            read = (char)data[i];
        }
        return username.toString();
    }

    public void addString(String add, int start){  // Add string to byte buff
        for(int i = start; i < add.length() + start; i++)
            buf[start] = (byte)add.charAt(i);
        buf[add.length()+start] = '#';
    }

    private void broadcast() {
        reply.setData(data);
        // TODO: Setup that packet is broadcasted
    }

    private void whisper() {
        int destinationToken = (int)data[1];
    //    InetAddress address = Server.getAddress(destinationToken); //TODO change
   //     reply.setData(data);
   //     reply.setAddress(address);
    }
}
